<?php
/**
 * 所属项目 110.
 * 开发者: tangtanglove
 * 创建日期: 2014-11-18
 * 创建时间: 10:09
 * 版权所有 869716224@qq.com
 */

namespace Admin\Controller;


use Admin\Builder\AdminListBuilder;
use Think\Controller;

class ThemeController extends Controller
{

    public function lists()
    {

        $listBuilder = new AdminListBuilder();

        $dir = './Theme/';
        $tplList = null;
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                    //去掉"“.”、“..”以及带“.xxx”后缀的文件
                    if ($file != "." && $file != ".." && !strpos($file, ".")) {
                        if (is_file('./Theme/'  . $file . '/info.php')) {
                            $tpl = require_once('./Theme/'  . $file . '/info.php');
                            $tpl['path']='./Theme/' . $file;
                            $tpl['file_name'] = $file;
                            $tplList[] = $tpl;
                        }
                    }
                }
                closedir($dh);
            }
        }

        //获取当前使用主题
        $currentTheme = M('Config')->where(array('name'=>'SITE_THEME'))->getField('value');

        foreach ($tplList as &$theme) {
            if ($theme['file_name']==$currentTheme) {
                $theme['name'] = '<a style="color: green;font-weight: bold">√</a>' . $theme['name'];
                $theme['do'] = '<a class="btn">正在使用</a> <a class="btn btn-error "  onclick="themeManager.uninstall(\'' . $theme['file_name'] . '\')"><span style="color: white;font-weight: bold">×</span>使用默认</a>';
            } else {
                $theme['name'] = '<span style="color: red;font-weight: bold">×</span>' . $theme['name'];
                $theme['do'] = '<a class="btn" onclick="themeManager.install(\'' . $theme['file_name'] . '\')"><span style="color: green;font-weight: bold">√</span> 安装</a>';
            }
            if($theme['is_com']){
                $theme['is_com']='<strong style="color: orange">商业主题</strong>';
            }else{
                $theme['is_com']='<strong style="color: green">免费主题</strong>';
            }
        }
        unset($theme);

        $listBuilder->data($tplList);
        $listBuilder->title('主题管理');
        $listBuilder->keyText('name', '主题名称')->keyText('title', '标题')->keyText('summary', '模块介绍')
            ->keyText('version', '版本号')->keyText('is_com', '主题类型')
            ->keyLink('developer', '开发者', '{$website}')
            ->keyText('do', '操作');
        $listBuilder->display();
    }

    public function uninstall()
    {

        $result = M('Config')->where(array('name'=>'SITE_THEME'))->save(array('value'=>'default'));
        if ($result === true || $result) {
            $this->success('卸载主题成功', 'refresh');
        } else {
            $this->error('卸载主题失败' . $result['error_code']);
        }
    }

    public function install()
    {
        $value  = I('post.name');
        $result = M('Config')->where(array('name'=>'SITE_THEME'))->save(array('value'=>$value));
        if ($result === true || $result) {
            $this->success('安装主题成功', 'refresh');
        } else {
            $this->error('安装主题失败' . $result['error_code']);
        }
    }    


} 